let domain = module.exports;
let logger = require('pomelo-logger').getLogger('pomelo');
let parameterServices = require('../../../services/configServices');
let commonDao = require('../../../dao/commonDao');
let moment = require('moment');

domain.afterStartup = async function (cb) {
    await domain.loadParameter();
    await commonDao.updateAllData("userModel", { $or: [{ roomID: { $ne: "" } }, { frontendId: { $ne: "" } }] }, { roomID: "", frontendId: "" });
    cb();
};

domain.afterStartAll = async function () {


    let unionData = await commonDao.findData('unionModel', { 'isDel': false });
    for (let i = 0; i < unionData.length; i++) {
        //保存营收数据
        let dateTime = moment().format('YYYY-MM-DD');
        let createTime = moment().format('x');
        let unionID = unionData[i].unionID;
        let isExit = await commonDao.findOneData('teamDataModel', { unionID, dateTime });
        if (!isExit) {
            await commonDao.findOneAndUpdateEx('teamDataModel', { unionID, dateTime }, { unionID, dateTime, createTime, userCount: 0, drawCount: 0, drawTotalCount: 0, diamondCount: 0 }, { new: true, upsert: true });
        }
    }
    await commonDao.updateDataEx('uniqueIDModel', { key: 1 }, { $setOnInsert: { unionInviteID: 21130144 } }, { upsert: true });
};

domain.loadParameter = async function () {
    await parameterServices.loadConfig();
    logger.debug("http load parameter finished");
};